package com.guigu.tms.aggregation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guigu.tms.commons.aggregation.entity.Order;
import com.guigu.tms.aggregation.mapper.OrderMapper;
import com.guigu.tms.aggregation.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.aggregation.mapper.OrderLocationMapper;
import com.guigu.tms.aggregation.mapper.TransportOrderMapper;
import com.guigu.tms.aggregation.service.IOrderCargoService;
import com.guigu.tms.aggregation.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.aggregation.service.ITaskPickupDispatchService;
import com.guigu.tms.commons.aggregation.entity.Order;
import com.guigu.tms.commons.aggregation.entity.OrderLocation;
import com.guigu.tms.commons.order.entity.OrderCargo;
import com.guigu.tms.commons.order.entity.TransportOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wj
 * @since 2022-10-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IOrderCargoService cargoService;
    @Autowired
    private OrderLocationMapper orderLocationMapper;
    @Autowired
    private ITaskPickupDispatchService pickupDispatchService;

    @Autowired
    private TransportOrderMapper transportOrderMapper;

    @Override
    public int UpdateOrder(String end, String orderId) {
        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.eq("order_id", orderId);
        wrapper.set("current_agency_id", end);
        return orderMapper.update(null, wrapper);
    }


    @Override
    public Order getOrderById(String id) {
        //根据订单编号查询查询订单
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",id);
        Order order = orderMapper.selectOne(wrapper);
        //根据订单编号查询货物信息
        order.setOrderCargo(cargoService.getCargoByOrderId(id));
        //更具订单编号查询订单位置信息
        QueryWrapper<OrderLocation> wrapper1=new QueryWrapper<>();
        wrapper1.eq("order_id",id);
        order.setOrderLocation(orderLocationMapper.selectOne(wrapper1));
        //根据订单编号查询取派件信息
        order.setTaskPickup(pickupDispatchService.getTaskPickupDispatchByOrderId(id,1));
        order.setTaskDispatch(pickupDispatchService.getTaskPickupDispatchByOrderId(id,2));
        System.out.println(order);
        return order;
    }

    @Override
    public PageInfo<Order> getOrders(Order order, Integer num, Integer size) {
        PageHelper.startPage(num!=null?num:1,size!=null?size:5);
        System.out.println(order);
        List<Order> orders = orderMapper.selectOrders(order);
        for (Order order1 : orders) {
            //获取货物信息
            order1.setOrderCargo(cargoService.getCargoByOrderId(order1.getOrderId()));
            //获取订单位置信息
            QueryWrapper wrapper=new QueryWrapper<>();
            wrapper.eq("order_id",order1.getOrderId());
            order1.setOrderLocation(orderLocationMapper.selectOne(wrapper));
            //获取运单信息
            order1.setTransportOrder(transportOrderMapper.selectOne(wrapper));
            //获取取件信息
            order1.setTaskPickup(pickupDispatchService.getTaskPickupDispatchByOrderId(order1.getOrderId(),1));
            //获取派件信息
            order1.setTaskDispatch(pickupDispatchService.getTaskPickupDispatchByOrderId(order1.getOrderId(),2));
        }
        return new PageInfo<Order>(orders);
    }
}
